﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Message_AddController : UserController
    {
        public IUser_messageService _user_message { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(string code,string send_save,string user_name,string title,string content)
        {
            //检查用户是否登录
            Users model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时!" });
            }
            //检查用户名
            if (string.IsNullOrEmpty(user_name) || !_users.Exists(user_name))
            {
                return Ok(new { status = 0, msg = "该用户不存在或已删除!" });
            }
            //检查标题
            if (string.IsNullOrEmpty(title))
            {
                return Ok(new { status = 0, msg = "请输入短消息标题!" });
            }
            //检查内容
            if (string.IsNullOrEmpty(content))
            {
                return Ok(new { status = 0, msg = "请输入短消息内容!" });
            }
            //保存数据
            User_message modelMessage = new User_message();
            modelMessage.type = 2;
            modelMessage.post_user_name = model.user_name;
            modelMessage.accept_user_name = user_name;
            modelMessage.title = title;
            modelMessage.content = content;
            _user_message.Add(modelMessage);
            if (send_save == "true") //保存到收件箱
            {
                modelMessage.type = 3;
                _user_message.Add(modelMessage);
            }
            return Ok(new { status = 1, msg = "发布短信息成功!" });
        }
    }
}